var allStackPoint = null;//到货记录
$(function () {
	$.when(
		$.get(baseURL+'csg/carrivalrecord/select?sPGuid='+GetUrlParam("sPGuid"),function (r) {
			allStackPoint = r.returnlist;
			vm.allStackPoint = allStackPoint;
        })
	).done(
		$("#jqGrid").jqGrid({
			url: baseURL + 'csg/cgoodsdetail/list?sPGuid='+GetUrlParam("sPGuid")+'&pGuid='+GetUrlParam("pGuid"),
			datatype: "json",
			colModel: [
				{ label: 'guid', name: 'guid', index: 'guid', width: 50, key: true,hidden:true },
				{ label: '所属堆放点', name: 'aRGuid', index: 'a_r_guid', width: 80,
					formatter: function (cellvalue, options, rowObject) {
						for (d in allStackPoint) {
							if (cellvalue == allStackPoint[d].guid) return allStackPoint[d].sPName;
						}
					}
				},
				{ label: '类别(材料/设备)', name: 'gDType', index: 'g_d_type', width: 80 },
				{ label: '所属设备/材料ID', name: 'mDGuid', index: 'm_d_guid', width: 80 },
				{ label: '到货数', name: 'gDNum', index: 'g_d_num', width: 80 },
				{ label: '供货商', name: 'gDBusine', index: 'g_d_busine', width: 80 },
				{ label: '物质编码', name: 'gDCode', index: 'g_d_code', width: 80 },
				{ label: '材料大类', name: 'gDBigtype', index: 'g_d_bigtype', width: 80 },
				{ label: '材料中类', name: 'gDMidtype', index: 'g_d_midtype', width: 80 },
				{ label: '材料标签', name: 'gDLabel', index: 'g_d_label', width: 80 },
				{ label: '材料名称', name: 'gDName', index: 'g_d_name', width: 80 },
				{ label: '主要技术参数', name: 'gDParameter', index: 'g_d_parameter', width: 80 },
				{ label: '购置单位', name: 'gDUnit', index: 'g_d_unit', width: 80 },
				{ label: '购置总价', name: 'gDAllprice', index: 'g_d_allprice', width: 80 },
				{ label: '购置单价', name: 'gDPrice', index: 'g_d_price', width: 80 },
				{ label: '单重（kg）', name: 'gDWeight', index: 'g_d_weight', width: 80 },
				{ label: '状态', name: 'gDDatastate', index: 'g_d_datastate', width: 80,hidden:true }
			],
			viewrecords: true,
			height: 385,
			rowNum: 10,
			rowList : [10,30,50],
			rownumbers: true,
			rownumWidth: 25,
			autowidth:true,
			multiselect: true,
			pager: "#jqGridPager",
			jsonReader : {
				root: "page.list",
				page: "page.currPage",
				total: "page.totalPage",
				records: "page.totalCount"
			},
			prmNames : {
				page:"page",
				rows:"limit",
				order: "order"
			},
			gridComplete:function(){
				//隐藏grid底部滚动条
				$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
			}
		})
    )
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        allStackPoint:allStackPoint,
		cGoodsDetail: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.cGoodsDetail = {};
		},
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(guid)
		},
		saveOrUpdate: function (event) {
			var pGuid = GetUrlParam("pGuid");
			var sPGuid = GetUrlParam("sPGuid");
			var url = vm.cGoodsDetail.guid == null ? "csg/cgoodsdetail/saveNew/"+sPGuid+"/"+pGuid : "csg/cgoodsdetail/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.cGoodsDetail),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "csg/cgoodsdetail/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "csg/cgoodsdetail/info/"+guid, function(r){
                vm.cGoodsDetail = r.cGoodsDetail;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});